function DicManager() {

}

/**
 * 字典初始化
 */
DicManager.prototype.init = function() {
	var dicRequest = DicManager.prototype.getDicNeedful();
	if (dicRequest && dicRequest.length > 0) {
		DicManager.prototype.getDicFromServer(dicRequest);
	}
}

/**
 * 获取需要请求的字典值
 * @return {String} 字典值字符串 
 */
DicManager.prototype.getDicNeedful = function() {
	var dicRequest = [];
	var dicRenderMap = {};
	$("[dicCode]").each(function(k, v) {
		var dicType = DicManager.prototype.getDicType(v);
		if (dicType) {
			dicRequest.push(dicType);
			dicRenderMap[$(v).attr("field")] = dicType;
		}
	});
	window.dicRenderMap = dicRenderMap;
	return dicRequest.join(",");
}

/**
 * 请求字典
 * @param {String} dicRequest 字典值字符串
 */
DicManager.prototype.getDicFromServer = function(dicRequest) {
	$.ajax( {
		url : serverPath + "/base/dic/getMutiDic.do?dicType=" + dicRequest,
		type : "GET",
		dataType : "json",
		async : false,
		success : function(data, textStatus, jqXHR) {
			DicManager.prototype.cacheDicData(data);
		}
	});
}

/**
 * 缓存字典
 * @param {Map} 字典map
 */
DicManager.prototype.cacheDicData = function(data) {
	if (data) {
		var dicMap = {};
		$.each(data, function(k, v) {
			if (v) {
				var subMap = {};
				$.each(v, function(k, v) {
					subMap[v.code] = v.codeValue;
				})
				dicMap[k] = subMap;
			}
		})
		window.dicMap = dicMap;
	}
}

/**
 * grid字典解释器
 * @param {Object} e 当前列数据
 * @return {String} 字典值 
 */
DicManager.prototype.dicRender = function(e) {
	var dicType = DicManager.prototype.getDicTypeFromCache(e);
	if (!dicType) {
		return e.value;
	}
	return DicManager.prototype.getDicValue(dicType, e.value);
}

/**
 * 获取缓存字典值
 * @param {String} dicType 字典类型
 * @param {String} code 字典代码
 * @return {String} 字典值
 */
DicManager.prototype.getDicValue = function(dicType, code){
	var dicMap = window.dicMap;
	if(dicMap && dicMap[dicType] && dicMap[dicType][code]){
		return dicMap[dicType][code];
	}else{
		return code;
	}
}

/**
 * 获取字典类型
 * @param {Object} e 对象
 * @return {String} 字典类型 
 */
DicManager.prototype.getDicType = function(e) {
	var jqE = $(e);
	var type = jqE.attr("dicCode");
	var field = jqE.attr("field");
	jqE = null;
	if (!type) {
		type = field;
	}
	return type;
}

/**
 * 获取字典类型
 * @param {Object} e 对象
 * @return {String} 字典类型 
 */
DicManager.prototype.getDicTypeFromCache = function(e) {
	var jqE = $(e);
	var field = jqE.attr("field");
	var dicRenderMap = window.dicRenderMap;
	if(dicRenderMap && dicRenderMap[field]){
		return dicRenderMap[field];
	}
	return field;
}